package work.mediway.mdm.controller.biz;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.mediway.hos.app.base.seure.util.SecurityUtils;
import com.mediway.hos.base.model.BaseResponse;
import com.mediway.hos.log.annotation.OperLog;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

import cn.hutool.core.util.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import work.mediway.mdm.core.page.QueryRequest;
import work.mediway.mdm.core.annotation.OpsLog;
import work.mediway.mdm.core.enums.OpsLogTypeEnum;
import work.mediway.mdm.biz.entity.biz.BpmDict;
import work.mediway.mdm.biz.service.biz.BpmDictService;

/**
 * 字典流程申请
 *
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2022/8/10
 */
@Api(tags = "字典流程申请")
@ApiSupport(order = 28)
@Validated
@RequestMapping("dict/process")
@RestController
@RequiredArgsConstructor
public class BpmDictProcessApplyController {

    private final BpmDictService bpmDictService;

    @ApiOperation(value = "创建业务字典申请")
    @ApiOperationSupport(order = 1)
    @OperLog(title = "创建业务字典申请", content = "创建业务字典申请")
    @OpsLog(value = "查询典消息所有列表", type = OpsLogTypeEnum.INSERT)
    @PostMapping()
    public BaseResponse<Long> createDictApply(@Valid @RequestBody BpmDict bpmDict) {
        Long id = this.bpmDictService.createDictApply(Long.valueOf(SecurityUtils.getUserId()), bpmDict);
        return BaseResponse.success(id);
    }

    @GetMapping()
    @ApiOperation("获得业务字典申请")
    @ApiImplicitParam(name = "id", value = "编号", paramType = "query")
    @ApiOperationSupport(order = 3)
    public BaseResponse<Page<BpmDict>> getLeave(Long id, QueryRequest queryRequest) {
        Page<BpmDict> page = new Page<>(queryRequest.getCurrent(), queryRequest.getSize());
        LambdaQueryWrapper<BpmDict> wrapper = Wrappers.lambdaQuery(BpmDict.class).eq(BpmDict::getDeleted, false);
        if (ObjectUtil.isNotEmpty(id)) {
            wrapper.eq(BpmDict::getId, id);
        }
        Page<BpmDict> bpmDicts = this.bpmDictService.getBaseMapper().selectPage(page, wrapper);
        return BaseResponse.success(bpmDicts);
    }

}
